<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Dingo\Api\Exception\StoreResourceFailedException;
use JWTAuth;

class AbstractApiRequest extends FormRequest
{
    // 错误时抛出异常
    protected function failedValidation(Validator $validator)
    {
        $message = $validator->errors()->first();
        $res = array(
            'msg'=>$message,
            'status_code' => 200
        );
        $headers = array(
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Headers' => 'Origin, Content-Type, Cookie, Accept',
            'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, OPTIONS',
            'Access-Control-Allow-Credentials' => 'true',
        );
        throw new StoreResourceFailedException($message, $res, null ,$headers);
    }

    /**
     * 获取登录人的基本信息
     * @return [type] [description]
     */
    protected function getLoginInfo(){
        $user = JWTAuth::parseToken()->authenticate();
        return $user;
    }

}
